 package start.controller;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 公共的文件上传控制器
 * @author ma
 *
 */
@RestController
public class FileUploadCtrl {
	@RequestMapping("/fileupload/upload")
	public String upload(@RequestParam("file")MultipartFile file,HttpServletRequest request) {
		String filename="";
		try {
		String path = request.getServletContext().getRealPath("/files");
		//获取网站服务器绝对路径
		String oname = file.getOriginalFilename();//获取原始文件名
		oname = oname.substring(oname.lastIndexOf("."));//获取原始文件名扩展名
		
		long t = System.currentTimeMillis(); //获取当前系统时间haomiao shu
		File dest= new File(path+"\\"+t+oname);
		
		//File dest = new File("D:\\abc\\");//绝对路径  服务器内存
		file.transferTo(dest);//把内存中的文件换华为物理文件
		filename = t+oname;
		}catch(Exception e){
			
			e.printStackTrace();
		}
		return filename;
		
	}
	
}
